package com.xsp.statistics.schedule;

import com.xsp.statistics.service.StatisticsDailyService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Component
public class ScheduleTask {

    @Autowired
    StatisticsDailyService dailyService;

    //每日凌晨零点重新获取前一天的统计数据
    @Scheduled(cron = "0 0 0 1/1 * ?")
    public void task(){
        //拿到前一天的日期
        //设置日期模板
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        //改为前一天时间
        calendar.add(Calendar.DATE,-1);
        //将日期时间进行格式化,得到日期
        String frontDay = dateFormat.format(calendar.getTime());

        //获取统计数据
        dailyService.registerCount(frontDay);
    }

}
